<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
            这个题可以使用「打劫劫舍」的思路去做
        */

      /**
       * @param {number[]} nums
       * @return {number}
       */
      var deleteAndEarn = function (nums) {
        let maxVal = 0;
        for (let n of nums) {
          maxVal = Math.max(n, maxVal);
        }
        const sums = new Array(maxVal + 1).fill(0);
        for (let n of nums) {
          sums[n] += n;
        }
        return rob(sums);
      };

      function rob(cost) {
        let f = cost[0];
        let r = Math.max(cost[0], cost[1]);
        for (let i = 2; i < cost.length; i++) {
          const temp = r;
          r = Math.max(cost[i] + f, temp);
          f = temp;
        }

        return r;
      }

      console.log(deleteAndEarn([3, 4, 2]));
    </script>
  </body>
</html>
